package com.base.monitor.module.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.base.monitor.framework.exception.CustomerException;
import com.base.monitor.framework.properties.SystemProperties;
import com.base.monitor.framework.result.PageResult;
import com.base.monitor.framework.util.CustomerUtils;
import com.base.monitor.framework.util.DiskUtils;
import com.base.monitor.model.enums.code.impl.common.ResultCode;
import com.base.monitor.model.enums.code.impl.system.UserCode;
import com.base.monitor.model.model.system.criteria.UserCriteria;
import com.base.monitor.model.model.system.dto.UserSaveDto;
import com.base.monitor.model.model.system.entity.User;
import com.base.monitor.module.system.properties.UserProperties;
import com.base.monitor.module.system.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: tsing
 * @create: 2021-01-21 22:21
 */
@Service
@EnableConfigurationProperties(UserProperties.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private DiskUtils diskUtils;

    @Autowired
    private UserProperties userProperties;

    private static final List<User> userList = new ArrayList<>();

    @PostConstruct
    public void init() {
        initUserList();
    }

    private void initUserList() {
        User user = new User();
        BeanUtils.copyProperties(userProperties, user);
        user.setPassword("");

        userList.clear();
        userList.add(user);
    }

    /**
     * 分页查询用户信息
     * @param userCriteria 查询条件
     * @return
     */
    @Override
    public PageResult<User> page(UserCriteria userCriteria) {
        List<User> result = new ArrayList<>(userList);
        if (StrUtil.isNotBlank(userCriteria.getUsername())) {
            result = result.stream()
                    .filter(item -> item.getUsername().contains(userCriteria.getUsername()))
                    .collect(Collectors.toList());
        }
        if (StrUtil.isNotBlank(userCriteria.getMobile())) {
            result = result.stream()
                    .filter(item -> item.getMobile().contains(userCriteria.getMobile()))
                    .collect(Collectors.toList());
        }
        return new PageResult<>(result.size(), CustomerUtils
                .getSubListForPage(result, userCriteria.getPage(), userCriteria.getSize()));
    }

    /**
     * 根据id查询用户信息
     *
     * @param id 用户id
     * @return
     */
    @Override
    public User findOne(Long id) {
        List<User> result = userList.stream()
                .filter(item -> item.getId().equals(id)).collect(Collectors.toList());
        if (result.size() != 0) {
            return result.get(0);
        }
        return null;
    }

    /**
     * 更新用户信息
     *
     * @param userSaveDto 用户信息
     */
    @Override
    public void edit(UserSaveDto userSaveDto) {
        // 1.校验参数
        if (userSaveDto.getId() == null) {
            throw new CustomerException(ResultCode.HANDLE_DATA_NOT_EXISTED, "更新时，用户id不能为空");
        }
        List<User> result = userList.stream().filter(item -> item.getId().equals(userSaveDto.getId())).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(result)) {
            throw new CustomerException(ResultCode.RETURN_DATA_NOT_FOUND, "更新时，没有找到需要更新的用户");
        }

        // 2.封装数据
        BeanUtils.copyProperties(userSaveDto, userProperties);
        initUserList();

        // 3.进行保存
        diskUtils.updatePropertiesFile("user.info.id", userProperties.getId() + "");
        diskUtils.updatePropertiesFile("user.info.username", userProperties.getUsername());
        diskUtils.updatePropertiesFile("user.info.roleName", userProperties.getRoleName());
        diskUtils.updatePropertiesFile("user.info.mobile", userProperties.getMobile());
        diskUtils.updatePropertiesFile("user.info.createTime", userProperties.getCreateTime());
    }
}
